#include <string.h>
#include <iostream>
#include <vector>
#if 0
using std::cout;
using std::endl;
using std::cin;
using std::vector;
#endif

using namespace std;

class String{
public:
    //无参构造函数
    String()
        :_pstr(new char[1]())
    {

    }
    //有参构造函数
    String(const char *pstr)
        :_pstr(new char [strlen(pstr)+1])
    {
        strcpy(_pstr,pstr);
    }
    //拷贝构造函数
    String(const String & rhs)
        :_pstr(new char [strlen(rhs._pstr)+1]())
    {
        strcpy(_pstr,rhs._pstr);
    }
    //赋值运算符函数
    //s1 = s2;
    String & operator=(const String &rhs){
        if(this != &rhs){
            delete [] _pstr;
            _pstr = new char[strlen(rhs._pstr)+1]();
            strcpy(_pstr,rhs._pstr);
        }
        return *this;
    }
    //赋值运算符函数
    //s1 = "hwllo";
    String &operator=(const char *pstr){
        String temp = String(_pstr);
        *this = temp;
        return *this;
    }

    //析构函数
    ~String(){
        if(_pstr != nullptr){
            delete [] _pstr;
            _pstr =nullptr;
        }
    }

    String &operator+=(const String &rhs){
        char *temp = new char[strlen(_pstr)+strlen(_pstr)+1]();
        strcpy (temp ,_pstr);
        strcat(temp,rhs._pstr);
        delete [] _pstr;
        _pstr = temp;
        return *this;
    }
    String &operator+=(const char *pstr){
        if(pstr != nullptr){
            String temp = String(pstr);//String(pstr)临时对象
            *this += temp;
        }
        return *this;
    }

    const char &operator[](size_t index)const{

    }


    friend ostream& operator<<(ostream& os, const String& rhs);
    friend istream& operator>>(istream& is, String& rhs);

    friend bool operator==(const String &lhs, const String &rhs);
    friend bool operator!=(const String &lhs, const String &rhs);

    friend bool operator<(const String &lhs, const String &rhs);
    friend bool operator>(const String &lhs, const String &rhs);
    friend bool operator<=(const String &lhs, const String &rhs);
    friend bool operator>=(const String &lhs, const String &rhs);
private:
    char *_pstr;
};

String operator+(const String &lhs,const String &rhs){
    String temp(lhs);
    temp += rhs;
    return temp;
}
String operator+(const String & lhs, const char * pstr){
    String temp(lhs);
    temp += pstr;
    return temp;
}
String operator+(const char * pstr, const String & rhs){
    String temp(pstr);//采用有参构造函数
    temp += rhs;
    return temp;
}

ostream& operator<<(ostream& os, const String& rhs) {
    if(rhs._pstr){
        os << rhs._pstr;
    }
    return os;
}
istream& operator>>(istream& is, String& rhs) {
    if(rhs._pstr){
        is >> rhs._pstr;
    }
    return is;
}


void test(){
    String s1("xww");
    String s2("128");
    cout << s1 << endl;
    cout << s2 << endl;

}

int main()
{
    test();
    return 0;
}
